package com.example.studentmanagement;

import androidx.appcompat.app.AppCompatActivity;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import com.example.studentmanagement.dao.StudentDao;
import com.example.studentmanagement.entity.Student;
import com.example.studentmanagement.util.DBUtil;
import com.example.studentmanagement.util.ToolsUtil;

public class AddStudent extends AppCompatActivity {

    private StudentDao studentdao;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addstudent);

        DBUtil dbUtil = new DBUtil(AddStudent.this);
        SQLiteDatabase db = dbUtil.getWritableDatabase();
        DBUtil.db=db;


        EditText useridD=this.findViewById(R.id.add_userid);
        EditText pwdD=this.findViewById(R.id.add_password);
        EditText nameE=this.findViewById(R.id.add_username);
        EditText proO=this.findViewById(R.id.add_pro);
        EditText sexX=this.findViewById(R.id.add_sex);
        Button add=this.findViewById(R.id.add_button_add);

        studentdao = new StudentDao();

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String userid= ToolsUtil.editTextToString(useridD);
                String pwd= ToolsUtil.editTextToString(pwdD);
                String name= ToolsUtil.editTextToString(nameE);
                String pro= ToolsUtil.editTextToString(proO);
                String sex= ToolsUtil.editTextToString(sexX);

                if (userid.isEmpty() || pwd.isEmpty() || name.isEmpty() || pro.isEmpty() || sex.isEmpty()) {
                    Toast.makeText(AddStudent.this,"所有所有选项均为必填项",Toast.LENGTH_SHORT).show();
                    return;
                }
                Student student = new Student(userid , pwd , name , pro , sex);
                long insert = studentdao.insertStudent(student);
                // 检查输入是否有效，然后添加学生
                if(insert != -1){
                    Toast.makeText(AddStudent.this , "添加成功" , Toast.LENGTH_SHORT).show();
                    onBackPressed();
                }else {
                    Toast.makeText(AddStudent.this , "添加失败" , Toast.LENGTH_SHORT).show();
                }


            }
        });
    }

}